#include "rgb.h"

void Rgb_Port_Init(void)
{
    #if RGB_PWM
        PWM_Init(PWM_PRESSEL_FSYS_D1,1000);
        /*PWM0 独立模式*/PWM_IndependentModeConfig(PWM0,0);
        /*PWM1 独立模式*/PWM_IndependentModeConfig(PWM1,0);
        /*PWM2 独立模式*/PWM_IndependentModeConfig(PWM2,0);
        PWM_OutputStateConfig(0x00|PWM0|PWM1|PWM2,PWM_OUTPUTSTATE_ENABLE);
        /*PWM波形无反相*/PWM_PolarityConfig(0x00|PWM0|PWM1|PWM2, PWM_POLARITY_NON_INVERT);
        PWM_PWM0Selection(PWM0_OutputPin_P00);
        PWM_PWM1Selection(PWM1_OutputPin_P01);
        PWM_PWM2Selection(PWM2_OutputPin_P27);
        PWM_ITConfig(DISABLE,LOW);
        PWM_Cmd(ENABLE);

    #else
		#if CHANGE_COLOR
			GPIO_InitType GPIO_InitStructure;
			RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);

			GPIO_InitStruct(&GPIO_InitStructure);
			GPIO_InitStructure.Pin = RGB_R_PIN | RGB_B_PIN | RGB_G_PIN;
			GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUTPUT_PP;
			GPIO_InitStructure.GPIO_Current = GPIO_DC_LOW;
			GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
			RGB_R = 0;
			RGB_B = 0;
			RGB_G = 0;
		#else
				GPIO_Init(GPIO0, GPIO_PIN_1, GPIO_MODE_OUT_PP);
				DEV_LED = 0;
		#endif
    #endif
}

// ==================================================================
/// @brief  : 灯条引脚处理  id 操作的pwm口, pwm_set 占空比
//  (pwm_set 占空比取值范围是 0 ~ 255),
//  当没有选用PWM时,不需要定义此函数(不需要做修改)
// ==================================================================
void Rgb_Opt_Port(rgb_u8_t id, rgb_u16_t pwm_set)
{
    #if RGB_PWM
        #if (CHANGE_COLOR && LIGHT_GRADE)
            if (!pwm_set) {
                if (DEV_R_LED == id) {
                    PWM_IndependentModeConfig(RGB_R, pwm_set);
                } else if (DEV_G_LED == id) {
                    PWM_IndependentModeConfig(RGB_G, pwm_set);
                } else if (DEV_B_LED == id) {
                    PWM_IndependentModeConfig(RGB_B, pwm_set);
                }
            } else {
                if (DEV_R_LED == id) {
                    PWM_IndependentModeConfig(RGB_R, pwm_set - rgb_para.light_cnt);
                } else if (DEV_G_LED == id) {
                    PWM_IndependentModeConfig(RGB_G, pwm_set - rgb_para.light_cnt);
                } else if (DEV_B_LED == id) {
                    PWM_IndependentModeConfig(RGB_B, pwm_set - rgb_para.light_cnt);
                }
            }

        #elif (CHANGE_COLOR)                                                   //七彩且光照强度不可调
            if (DEV_R_LED == id) {
                PWM_IndependentModeConfig(RGB_R, pwm_set);
            } else if (DEV_G_LED == id) {
                PWM_IndependentModeConfig(RGB_G, pwm_set);
            } else if (DEV_B_LED == id) {
                PWM_IndependentModeConfig(RGB_B, pwm_set);
            }

        #endif


    #endif
}


